/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.service.impl;

import com.bifrost.constant.OrderReturnStatus;
import com.bifrost.constant.OrderStatus;
import com.bifrost.constant.OrderType;
import com.bifrost.entity.Order;
import com.bifrost.entity.OrderReturn;
import com.bifrost.entity.RefundRequest;
import com.bifrost.repository.OrderRepository;
import com.bifrost.repository.OrderStatusHistoryRepository;
import com.bifrost.repository.RefundRequestRepository;
import com.bifrost.service.OrderService;
import com.bifrost.service.RefundRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author joewee
 * @version 1.0.0
 * @Date 2017/12/4 13:30
 */
@Service
public class RefundRequestServiceImpl implements RefundRequestService {
    @Autowired
    RefundRequestRepository refundRequestRepository;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderRepository orderRepository;
    @Autowired
    OrderStatusHistoryRepository orderStatusHistoryRepository;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean buildRefundRequest(Order order) {
        if (OrderStatus.PAID.equalsIgnoreCase(order.getOrderStatus()) || OrderStatus.CONFIRMED.equalsIgnoreCase(order.getOrderStatus())) {
            RefundRequest refundRequest = new RefundRequest();
            refundRequest.setOrderTotalFee(order.getOrderTotal().multiply(BigDecimal.valueOf(100L)).intValue());
            refundRequest.setOutTradeNumber(order.getOutTradeNo());
            refundRequest.setPaymentId(order.getPayment().getPaymentTypeId());
            refundRequest.setRefundFee(refundRequest.getOrderTotalFee());
            refundRequest.setRefundDesc(order.getCustomerMessage());
            refundRequest.setTimestamp(new Date());
            refundRequest.setOrderId(order.getOrderId());
            refundRequest.setFromOrder(Boolean.TRUE);
            refundRequestRepository.save(refundRequest);
            //取消订单移至退款中执行
            return true;
        } else {
            throw new RuntimeException(String.format("订单{}不能创建退款申请", order.getOrderNumber()));
        }
    }

    @Override
    public boolean buildRefundRequest(OrderReturn orderReturn) {
        //暂时只有优惠券退单才能创建退款申请
        if (OrderReturnStatus.CONFIRMED.equalsIgnoreCase(orderReturn.getStatus())
                && OrderType.CASH_BY_COUPON.equalsIgnoreCase(orderReturn.getRelatedOrder().getOrderType())) {
            RefundRequest refundRequest = new RefundRequest();
            refundRequest.setOrderTotalFee(orderReturn.getRelatedOrder().getOrderTotal().multiply(BigDecimal.valueOf(100L)).intValue());
            refundRequest.setOutTradeNumber(orderReturn.getRelatedOrder().getOutTradeNo());
            refundRequest.setPaymentId(orderReturn.getRelatedOrder().getPayment().getPaymentTypeId());
            refundRequest.setRefundFee(refundRequest.getOrderTotalFee());
            refundRequest.setRefundDesc(orderReturn.getCustomerMessage());
            refundRequest.setTimestamp(new Date());
            refundRequest.setReturnId(orderReturn.getId());
            refundRequest.setOrderId(orderReturn.getRelatedOrder().getOrderId());
            refundRequestRepository.save(refundRequest);
            //取消订单移至退款中执行
            return true;
        } else {
            throw new RuntimeException(String.format("退单{}不能创建退款申请", orderReturn.getReturnNumber()));
        }
    }
}
